//
//  ExNavigationViewController.m
//  OC-Example
//
//  Created by apang on 2018/7/31.
//  Copyright © 2018年 apang.men All rights reserved.
//

#import "ExNavigationViewController.h"

@interface CViewController: UIViewController

@end

@implementation CViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor redColor]];
    self.title = @"C";
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];

    [button setTitle:@"cButton" forState:UIControlStateNormal];

    [button setFrame:CGRectMake(20, 100, 100, 30)];

    [self.view addSubview:button];

    [button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
}
- (void)test {

    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

@interface BViewController: UIViewController

@end

@implementation BViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor cyanColor]];
    self.title = @"B";
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];

    [button setTitle:@"bButton" forState:UIControlStateNormal];

    [button setFrame:CGRectMake(20, 100, 100, 30)];

    [self.view addSubview:button];

    [button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
}
- (void)test {

    CViewController * cVC = [[CViewController alloc] init];

    id presentingViewController = self.presentingViewController;

    if ([presentingViewController isKindOfClass:[UINavigationController class]]) {

        [((UINavigationController *)presentingViewController).topViewController.navigationController pushViewController:cVC animated:YES];
        [self dismissViewControllerAnimated:YES completion:nil];

    }else if ([presentingViewController isKindOfClass:[UITabBarController class]]) {

        [((UINavigationController *)((UITabBarController *)presentingViewController).selectedViewController).topViewController.navigationController pushViewController:cVC animated:YES];

        [self dismissViewControllerAnimated:YES completion:nil];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


@interface ExNavigationViewController ()

@end

@implementation ExNavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.title = @"A";
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];

    [button setTitle:@"aButton" forState:UIControlStateNormal];

    [button setFrame:CGRectMake(20, 100, 100, 30)];

    [self.view addSubview:button];

    [button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
}

- (void)test {

    BViewController * bVC = [[BViewController alloc] init];

    [self presentViewController:bVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

